jQuery.support.cors = true // 解决IE ajax 进error

// 请求地址
const baseUrl = __ENV_CONFIG__.API

// 依赖jquery的请求
const http = {
  get: (url, data, callback, btn) => {
    $.ajax({
      type: 'GET',
      dataType: 'json',
      contentType: 'application/json',
      url,
      data,
      success: (res) => {
        http.changeBtn(btn, false)
        callback(res)
      },
      beforeSend: (xhr) => {
        http.changeBtn(btn, true)
      },
      complete: (xhr, msg) => {
        http.changeBtn(btn, false)
      },
      error: (e) => {
        http.changeBtn(btn, false)
        msg.error('请求异常')
      }
    })
  },
  post: (url, data, callback, btn) => {
    $.ajax({
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json',
      url,
      data: data ? JSON.stringify(data) : '',
      success: (res) => {
        http.changeBtn(btn, false)
        callback(res)
      },
      beforeSend: (xhr) => {
        http.changeBtn(btn, true)
      },
      complete: (xhr, msg) => {
        http.changeBtn(btn, false)
      },
      error: (e) => {
        http.changeBtn(btn, false)
        msg.error('请求异常')
      }
    })
  },
  /**
   * 改变按钮状态
   * @param {Object} btn
   * @param {Object} blen
   */
  changeBtn: (btn, blen) => {
    if (btn) {
      btn.attr('disabled', blen)
    }
  }
}